<template>
  <div class="request-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="9">
        <div>付款申请历史记录查询</div>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="15">
        <el-button
          style="z-index: 10;margin-left: 20px"
          icon="el-icon-download"
          type="success"
          :disabled="!selList.length"
          @click="importFile()"
        >
          导出
        </el-button>
        <el-form inline @submit.native.prevent>
          <el-form-item>
            <el-select v-model="queryForm.type" clearable placeholder="请选择付款状态">
              <el-option
                v-for="item in optionsList"
                :key="item.val"
                :label="item.name"
                :value="item.val"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.keyword"
              placeholder="请输入供应商名称"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="search">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" @selection-change="setSelectRow" >
      <el-table-column
        header-align="center"
        type="selection"
      >
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        type="index"
        width="95"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="subtotal"
        label="付款金额"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="mobile"
        label="手机号"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="servicetotal"
        label="服务费"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="supplier_name"
        label="服务商"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="create_time"
        label="付款申请时间"
        width="165"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="pay_time"
        label="付款完成时间"
        width="165"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="status"
        label="状态"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="deduction_amount"
        label="扣款金额"
        width="150"
      />
      <el-table-column align="center" label="操作" width="85">
        <template #default="{ row }">
          <el-button
            v-if="row.status == '已完税'"
            type="text"
            @click="showImg(row)"
          >
            查看完税证明
          </el-button>
          <el-button
            v-else
            disabled
            type="text"
          >
            暂无
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      title="完税证明"
      :visible.sync="imgDialogVisible"
      width="30%"
    >
      <el-image :src="wsImg" />
    </el-dialog>
  </div>
</template>

<script>
  import { getRequestList, queryImportFile } from '@/api/payment'

  export default {
    name: 'PaymentRequest',

    data() {
      return {
        optionsList: [
          {name: '已付款', val: 1},
          {name: '已完税', val: 2}
        ],
        list: [],
        selList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          page: 1,
          list_row: 10,
          keyword: '',
          type: '',
        },
        imgDialogVisible: false,
        wsImg: '',
      }
    },

    created() {
      this.getList()
    },

    activated() {
      this.getList()
    },

    methods: {
      importFile() {
        let params = {
          id: this.selList.map(val => val.id).join(',')
        }
        queryImportFile(params).then(res => {
          location.href = res
        })
      },
      setSelectRow(val) {
        this.selList = val
      },
      showImg(row) {
        this.wsImg = row.tax_img
        this.imgDialogVisible = true
      },
      async getList() {
        this.listLoading = true
        const { data, total } = await getRequestList(this.queryForm)
        console.log(data)
        this.list = data.map((item) => ({
          ...item,
          pay_time: item.pay_time || null,
        }))
        this.total = total
        this.listLoading = false
      },
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getList()
      },
      search() {
        this.queryForm.page = 1
        this.getList()
      },
      gotoRequest() {
        this.$router.push('/paymentPending')
      },
    },
  }
</script>

<style lang="scss" scoped></style>
